# This file contains the fastlane.tools configuration
# You can find the documentation at https://docs.fastlane.tools
#
# For a list of all available actions, check out
#
#     https://docs.fastlane.tools/actions
#
# For a list of all available plugins, check out
#
#     https://docs.fastlane.tools/plugins/available-plugins
# ios 大小优化设置
#https://mp.weixin.qq.com/s?__biz=MzA3NzM0NzkxMQ==&mid=2655372272&idx=1&sn=6513887ec421f57a0a614a584b720649&chksm=84e20ed8b39587ce1f52ef90b1345cff313041de4fc0e16e4abc76f3199c778890ea3848fec4&mpshare=1&scene=1&srcid=0531OWQhLc6U5DekiCVauBMz&sharer_sharetime=1624796341174&sharer_shareid=951adc5a242ed23e66be966c1bec21fa&exportkey=AWjxtVero1WZQ%2BgMq6Y1uG4%3D&pass_ticket=veYmGE9QOcxEEem3Vv%2BP2tzQD94%2Fj6FoZV5gNJy9%2F2V1ioLntj5TfzzyxL5fvm7u&wx_header=0#rd
# Uncomment the line if you want fastlane to automatically update itself
# update_fastlane
##在项目文件android目录下执行命令fastlane uploadFir生成IPA文件,并且会自动上传到Fir,发送到钉钉群通知
#使用flutter打包命令 flutter build ipa --release 
# 应用名称：用呗分期App 友盟AppKey 61a07941e014255fcb8d32ac

require 'net/http'
require 'uri'
require 'json'


default_platform(:ios)

platform :ios do

  desc "打包iOS安装包上传到fir"
  time = Time.new.strftime("%Y年%m月%d日%H时%M分%S秒")
  lane :uploadFir do
    # add actions here: https://docs.fastlane.tools/actions
   sh("flutter", "build", "ipa", "--release","--export-method=ad-hoc")
   fir_cli api_token:"d2f653948d859d2f06f88eb4d1d39e8f", specify_file_path:"../build/ios/ipa/飞象分期.ipa",need_release_id:true,dingtalk_access_token:"3a01c05738abaaeaf5c5e17d80d4cc5870410387126c5a5e2fbf0095a79977ef",dingtalk_custom_message:"钉钉群通知,App,更新iOS版本【安装包啦】!!",dingtalk_at_all:"true",dingtalk_at_phones:time
   puts "上传ios测试版到Fir成功===== 测试版=============\n"+time+"============================" 
  end

  desc "发送消息到飞书"
  lane :sendLarkTalk do
  markdown =  {
      "msg_type": "post",
      "content": {
          "post": {
              "zh_cn": {
                  "title": "亿荐App,更新ios版本测试包更新通知",
                  "content": [
                      [{
                          "tag": "text",
                          "text": time+"测试包有更新: "
                      },
                      {
                          "tag": "a",
                          "text": "请点击查看",
                          "href": "https://d.firim.pro/fu2b"
                      }
                      ]
                  ]
              }
          }
      }
   }

    larkTalk_url = "https://open.feishu.cn/open-apis/bot/v2/hook/fe90dcfc-0e75-4a2c-a975-2e18b88576ff"

    uri = URI.parse(larkTalk_url)
    https = Net::HTTP.new(uri.host, uri.port)
    https.use_ssl = true

    request = Net::HTTP::Post.new(uri.request_uri)
    request.add_field('Content-Type', 'application/json')
    request.body = markdown.to_json

    response = https.request(request)
    puts "------------------------------"
    puts "Response #{response.code} #{response.message}: #{response.body}"
end

end
